﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRCServer.Entities;

namespace IRCServer.Backend
{
    /// <summary>
    /// 
    /// </summary>
    public class ServerBackend
    {
        private static ServerBackend instance;

        private ServerBackend() { }

        public static ServerBackend Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ServerBackend();
                    instance.Users = new List<Entities.User>();
                    instance.ServerSessions = new List<Entities.ServerSession>();
                    instance.ClientSessions = new List<Entities.ClientSession>();
                }
                return instance;
            }
        }

        /// <summary>
        /// Gets or sets the users.
        /// </summary>
        /// <value>
        /// The users.
        /// </value>
        public List<Entities.User> Users { get; set; }

        /// <summary>
        /// Gets or sets the routing daemon.
        /// </summary>
        /// <value>
        /// The routing daemon.
        /// </value>
        public Entities.RoutingDaemon RoutingDaemon { get; set; }
        /// <summary>
        /// Gets or sets the server sessions.
        /// </summary>
        /// <value>
        /// The server sessions.
        /// </value>
        public List<ServerSession> ServerSessions { get; set; }

        /// <summary>
        /// Gets or sets the client sessions.
        /// </summary>
        /// <value>
        /// The client sessions.
        /// </value>
        public List<ClientSession> ClientSessions { get; set; }

        /// <summary>
        /// Gets the client session by nickname.
        /// </summary>
        /// <param name="nickname">The nickname.</param>
        /// <returns>Client session object.</returns>
        public ClientSession GetClientSessionByNickname(string nickname)
        {
            List<ClientSession> filteredSessions = ServerBackend.Instance.ClientSessions.Where(s => s.User.Nickname.ToLower() == nickname.ToLower()).ToList();

            if (filteredSessions.Count == 0)
                throw new Exception("Sessions are corrupted");

            return filteredSessions.First();
        }
    }
}
